package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bean.CmsType;
import com.woniu.bean.R;
import com.woniu.service.ICmsTypeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-06
 */
@RestController
@RequestMapping("/cmsType")
public class CmsTypeController {

    @Autowired
    private ICmsTypeService cmsTypeService;


    @ApiOperation(value = "业主分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "2"),
    })
    @CrossOrigin
    @GetMapping
    public R selectall(Integer currentPage, Integer pageSize) {
        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);
        R r= null;
        IPage<CmsType> iPage=new Page<>(currentPage,pageSize);
        IPage<CmsType> page = cmsTypeService.page(iPage);
        List<CmsType> records = page.getRecords();
        System.out.println("records = " + records);
        long total = page.getTotal();
        r=R.ok();
        Map map=new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        r.setData(map);
        return r;
    }
    @ApiOperation(value = "单个删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId",value = "需要删除的数据id",example = "1")
    })
    @CrossOrigin
    @DeleteMapping("/{typeId}")
    public R deleteById(@PathVariable Integer typeId) {

        System.out.println("typeId = " + typeId);


        R r=null;
        boolean b = cmsTypeService.removeById(typeId);
        if(b){
            r=R.ok();
        }

        return r;
    }
    @ApiOperation(value = "批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "需要删除的数据id的集合",example = "1")
    })
    @CrossOrigin
    @DeleteMapping
    public R deleteAll(@RequestBody List<Integer> ids) {
        System.out.println(ids);
        R r=null;
        boolean b = cmsTypeService.removeBatchByIds(ids);
        if(b){
            r=R.ok();
        }else {
            r=R.error();
        }

        return r;
    }
    @ApiOperation(value = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Owner",value = "添加数据对象",example = "1")
    })
    @CrossOrigin
    @PostMapping
    public R add(@RequestBody CmsType cmsType) {
        System.out.println("HouseController.add");
        boolean save = cmsTypeService.save(cmsType);
        R r=null;
        if(save){
            r=R.ok();
        } else {
            r=R.error();
        }
        return r;
    }

    @CrossOrigin
    @PutMapping
    public R updateDemp(@RequestBody CmsType cmsType) {
        System.out.println("building = " + cmsType);
        boolean b = cmsTypeService.updateById(cmsType);
        R r=null;
        if(b){
            r=R.ok();
        } else {
            r=R.error();
        }
        return r;
    }

    @CrossOrigin
    @GetMapping("/{typeId}")
    public CmsType selectById(@PathVariable Integer typeId) {

        System.out.println("typeId = " + typeId);

        CmsType cmsType = cmsTypeService.getById(typeId);
        return cmsType;
    }



}
